package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    EditText ed_username;
    EditText ed_password;
    CheckBox chk_checkBox;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        chk_checkBox = (CheckBox) findViewById(R.id.checkBox);

        ed_username = (EditText) findViewById(R.id.ed_username);
        ed_password = (EditText) findViewById(R.id.ed_userpassword);

        sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        editor = sharedPreferences.edit();

        String name = sharedPreferences.getString("Username", "");//取值
        if (name == null){//单选框判断
            chk_checkBox.setChecked(false);
        }else {
            chk_checkBox.setChecked(true);
            ed_username.setText(name);
        }
    }

    public void doClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                String name = ed_username.getText().toString().trim();
                String pass = ed_password.getText().toString().trim();
                if ("admin".equals(name) && "123456".equals(pass)){
                    if (chk_checkBox.isChecked()){
                        editor.putString("Username", name);
                        editor.commit();
                    }else {
                        editor.remove("Username");
                        editor.commit();
                    }
                    Toast.makeText(MainActivity2.this,"登陆成功！", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity2.this, "登陆失败", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    public void Jump1(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity2.this,MainActivity3.class);
        startActivity(intent);
    }
}